package steveshrader.budget.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import steveshrader.budget.shared.BudgetRequestFactory;
import steveshrader.budget.shared.ExpenseTypeProxy;
import steveshrader.budget.shared.PaymentTypeProxy;
import steveshrader.budget.shared.VendorProxy;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.web.bindery.requestfactory.shared.Receiver;


/**
 * Contains Helper methods used for UI Fields
 * @author steve.shrader
 */
public class FieldsHelper {
	public static final DateTimeFormat DATE_MONTH_FORMAT = DateTimeFormat.getFormat("MMMM yyyy");
	public static final DateBox.DefaultFormat DATE_BOX_CHART_FORMAT = new DateBox.DefaultFormat(DATE_MONTH_FORMAT);    //apply default format to DateBox
    public static final DateTimeFormat DATE_TIME_FORMAT = DateTimeFormat.getFormat("MM/dd/yyyy");
    public static final DateBox.DefaultFormat DATE_BOX_FORMAT = new DateBox.DefaultFormat(DATE_TIME_FORMAT);    //apply default format to DateBox
    private MultiWordSuggestOracle vendorOracle = new MultiWordSuggestOracle();     //list Vendor names for selection
    private MultiWordSuggestOracle expenseTypeOracle = new MultiWordSuggestOracle();     //list ExpenseType names for selection
    private MultiWordSuggestOracle paymentTypeOracle = new MultiWordSuggestOracle();     //list PaymentType names for selection
    private Map<String, VendorProxy> vendors = new HashMap<String, VendorProxy>();    //all Vendors in the DB to select from...mapped as vendorName->Vendor
    
    public FieldsHelper(BudgetRequestFactory requestFactory) {
        requestFactory.budgetServiceRequest().getVendors().fire(
        	new Receiver<List<VendorProxy>>() {
        		@Override
                public void onSuccess(List<VendorProxy> response) {
        			for (VendorProxy vendorProxy : response) {
						String vendorName = vendorProxy.getName();

						vendorOracle.add(vendorName);
						vendors.put(vendorName, vendorProxy);
					}
                }
            });
        
        requestFactory.budgetServiceRequest().getExpenseTypes().fire(
            	new Receiver<List<ExpenseTypeProxy>>() {
            		@Override
                    public void onSuccess(List<ExpenseTypeProxy> response) {
            			for (ExpenseTypeProxy expenseTypeProxy : response) {
    						String expenseTypeName = expenseTypeProxy.getName();

    						expenseTypeOracle.add(expenseTypeName);
    					}
                    }
                });
        
        requestFactory.budgetServiceRequest().getPaymentTypes().fire(
            	new Receiver<List<PaymentTypeProxy>>() {
            		@Override
                    public void onSuccess(List<PaymentTypeProxy> response) {
            			for (PaymentTypeProxy paymentTypeProxy : response) {
    						String paymentTypeName = paymentTypeProxy.getName();

    						paymentTypeOracle.add(paymentTypeName);
    					}
                    }
                });
    }
    
    public MultiWordSuggestOracle getVendorOracle() {
    	return vendorOracle;
    }
    
    public MultiWordSuggestOracle getExpenseTypeOracle() {
    	return expenseTypeOracle;
    }
    
    public MultiWordSuggestOracle getPaymentTypeOracle() {
    	return paymentTypeOracle;
    }
    
    /**
     * Try to determine the ExpenseType for a Vendor by using
     * the last one saved by the user.
     */
    public String getMostLikelyExpenseTypeForVendor(String vendorName) {
    	String expenseType = "";
    	VendorProxy vendor = vendors.get(vendorName);
    	if(vendor != null) {
    		expenseType = vendor.getLastExpenseType();
    	}
    	return expenseType;
    }
    
    /**
     * Try to determine the PaymentType for a Vendor by using
     * the last one saved by the user.
     */
    public String getMostLikelyPaymentTypeForVendor(String vendorName) {
    	String paymentType = "";
    	VendorProxy vendor = vendors.get(vendorName);
    	if(vendor != null) {
    		paymentType = vendor.getLastPaymentType();
    	}
    	return paymentType;
    }
}
